<?php
/* ******************************
 *	Product Catalog aJax actions
 * ******************************
 */
require_once '../../includes/classes/core.php';

$action		= $_REQUEST['act'];
$error		= '';
$data		= '';
$user_id	= $_SESSION['USERID'];


switch ($action) {
    case 'get_add_page':
		$page		= GetPage();
		$data		= array('page'	=> $page);
        break;   
        
    case 'get_edit_page':
	    $prod_id	= $_REQUEST['id'];
		$page		= GetPage($prod_id);
        $data 		= array('page'	=> $page);
        break; 
    case 'get_list':
	    $count = $_REQUEST['count'];
	    $hidden = $_REQUEST['hidden'];

	    $rResult = mysql_query("SELECT * FROM `transporter_company` where actived=1");

		$data = array(
			"aaData"	=> array()
		);

		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				/* General output */
				$row[] = $aRow[$i];
				if($i == ($count - 1)){
					$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
				}
			}
			$data['aaData'][] = $row;
		}
	break;      
        
        
        
    case 'save': 

    if ($_REQUEST[id]=='0') {
   mysql_query("INSERT INTO `transporter_company` 
              (`name`, `tin`) 
     VALUES   ('$_REQUEST[name]', '$_REQUEST[code]')"); 
    }  else {
   mysql_query("UPDATE `transporter_company` SET             
               `name`='$_REQUEST[name]', 
               `tin`='$_REQUEST[code]'            
                WHERE (`id`='$_REQUEST[id]')");
      }  
        
 
            break;
            
            
            
            
            
    case 'disable':
    mysql_query("UPDATE `transporter_company`
				 SET	`actived` = 0
				 WHERE	`id` = $_REQUEST[id]");
        break;
        
        
        
    default:
       $error = "Action is Null";
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	Product Catalog Functions
 * ******************************
 */


function GetPage($id)
{
    $res = mysql_fetch_array(mysql_query("SELECT * FROM `transporter_company` where id=$id"));
	$data = '
	<div id="dialog-form">
		<fieldset style="width: 450px; float: left;">
	    	<legend>ძირითადი ინფორმაცია</legend>

	    	<table class="dialog-form-table">
	    		<tr>
					<td style="width: 170px;"><label for="code">საიდენთიპიკაციო კოდი</label></td>
					<td>
						<input type="text" id="code" class="idle" style="display: inline;"  value="' . $res['tin'] . '" />
                        <button style="display: inline;" id="check_rs">შემოწმება</button>						   
					 </td>
				</tr>
	    		<tr>
					<td style="width: 170px;"><label for="part_number">დასახელება</label></td>
					<td>
						<input type="text" id="name" style="width: 260px;" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['name'] . '" />
					</td>
				</tr>
			</table>
        </fieldset> 
		<!-- ID -->
		<input type="hidden" id="prod_id" value="' . $res['id'] . '" />
    </div>

    ';
	return $data;
}

?>